﻿using System;
using System.Security.Cryptography;
using UMC.SshNet.Common;
using UMC.SshNet.Security.Cryptography.Ciphers;

namespace UMC.SshNet.Security.Cryptography
{
    /// <summary>
    /// Implements RSA digital signature algorithm.
    /// </summary>
    public class RsaDigitalSignature : CipherDigitalSignature, IDisposable
    {
        private HashAlgorithm _hash;

        /// <summary>
        /// Initializes a new instance of the <see cref="RsaDigitalSignature"/> class with the SHA-1 hash algorithm.
        /// </summary>
        /// <param name="rsaKey">The RSA key.</param>
        public RsaDigitalSignature(RsaKey rsaKey)
            : this(rsaKey, HashAlgorithmName.SHA1)
        { }

        /// <summary>
        /// Initializes a new instance of the <see cref="RsaDigitalSignature"/> class.
        /// </summary>
        /// <param name="rsaKey">The RSA key.</param>
        /// <param name="hashAlgorithmName">The hash algorithm to use in the digital signature.</param>
        public RsaDigitalSignature(RsaKey rsaKey, HashAlgorithmName hashAlgorithmName)
            : base(ObjectIdentifier.FromHashAlgorithmName(hashAlgorithmName), new RsaCipher(rsaKey))
        {
            switch (hashAlgorithmName.Name)
            {
                case "SHA":
                case "SHA1":
                case "System.Security.Cryptography.SHA1":
                    _hash = SHA1.Create();// new System.Security.Cryptography.SHA1Managed();
                    break;
                case "SHA256":
                case "SHA-256":
                case "System.Security.Cryptography.SHA256":
                    _hash =SHA256.Create();// new System.Security.Cryptography.SHA256Managed();
                    break;
                case "SHA384":
                case "SHA-384":
                case "System.Security.Cryptography.SHA384":
                    _hash = SHA384.Create();//. new System.Security.Cryptography.SHA384Managed();
                    break;
                case "SHA512":
                case "SHA-512":
                case "System.Security.Cryptography.SHA512":
                    _hash = SHA512.Create();//new System.Security.Cryptography.SHA512Managed();
                    break;

                case "System.Security.Cryptography.HMAC":
                case "HMACSHA1":
                case "System.Security.Cryptography.HMACSHA1":
                case "System.Security.Cryptography.KeyedHashAlgorithm":
                    _hash = new System.Security.Cryptography.HMACSHA1();
                    break;
                case "HMACSHA256":
                case "System.Security.Cryptography.HMACSHA256":
                    _hash = new System.Security.Cryptography.HMACSHA256();
                    break;
                case "HMACSHA384":
                case "System.Security.Cryptography.HMACSHA384":
                    _hash = new System.Security.Cryptography.HMACSHA384();
                    break;
                case "HMACSHA512":
                case "System.Security.Cryptography.HMACSHA512":
                    _hash = new System.Security.Cryptography.HMACSHA512();
                    break;
                default:
                    throw new ArgumentException($"Could not create {nameof(HashAlgorithm)} from `{hashAlgorithmName}`.", nameof(hashAlgorithmName));

                    // break;
            }
            // _hash = CryptoConfig.CreateFromName(hashAlgorithmName.Name) as HashAlgorithm
            //     ?? throw new ArgumentException($"Could not create {nameof(HashAlgorithm)} from `{hashAlgorithmName}`.", nameof(hashAlgorithmName));
        }

        /// <summary>
        /// Hashes the specified input.
        /// </summary>
        /// <param name="input">The input.</param>
        /// <returns>
        /// Hashed data.
        /// </returns>
        protected override byte[] Hash(byte[] input)
        {
            return _hash.ComputeHash(input);
        }

        #region IDisposable Members

        private bool _isDisposed;

        /// <summary>
        /// Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources.
        /// </summary>
        public void Dispose()
        {
            Dispose(disposing: true);
            GC.SuppressFinalize(this);
        }

        /// <summary>
        /// Releases unmanaged and - optionally - managed resources
        /// </summary>
        /// <param name="disposing"><c>true</c> to release both managed and unmanaged resources; <c>false</c> to release only unmanaged resources.</param>
        protected virtual void Dispose(bool disposing)
        {
            if (_isDisposed)
            {
                return;
            }

            if (disposing)
            {
                var hash = _hash;
                if (hash != null)
                {
                    hash.Dispose();
                    _hash = null;
                }

                _isDisposed = true;
            }
        }

        /// <summary>
        /// Releases unmanaged resources and performs other cleanup operations before the
        /// <see cref="RsaDigitalSignature"/> is reclaimed by garbage collection.
        /// </summary>
        ~RsaDigitalSignature()
        {
            Dispose(disposing: false);
        }

        #endregion
    }
}
